//
// Created by 32827 on 2023/12/6.
//

#ifndef YZFRAME_YZ_SPI_SD_H
#define YZFRAME_YZ_SPI_SD_H
#include "yz.h"
#include "yzl_softport_spi.h"
#define MicroSD_CS_SET yzf_gpio_set_1(YZF_GPIOB,YZF_GPIO_PIN_14)
#define MicroSD_CS_RESET yzf_gpio_set_0(YZF_GPIOB,YZF_GPIO_PIN_14)

#define MicroSD_READ_TOKEN 0b11111110
#define MicroSD_SINGLE_WRITE_TOKEN 0b11111110

#define MicroSD_MULTIPLE_WRITE_TOKEN_START 0b11111100
#define MicroSD_MULTIPLE_WRITE_TOKEN_END 0b11111101

extern YZF_SPI_POINTER MicroSD_SPI; 

enum SD_Response{
    SD_Response_0,
    SD_Response_1,
    SD_Response_2,
    SD_Response_3,
    SD_Response_6,
    SD_Response_7,
    SD_Response_NONE,
    SD_ERROR
};
char SD_Init(void);    //初始化函数

void SPI1_ReadWriteByteStart();
char SPI1_ReadWriteByte(char data);
void SPI1_ReadWriteByteEnd();
uint8_t *SentCommandToMicroSDV2(char cmd,uint32_t data,uint8_t crc,enum SD_Response res_type);

YZF_RESULT WriteSectorToMicroSD(long addr,YZF_RECV *area);//写一个扇区 512bit
YZF_RESULT WriteSectorsToMicroSD(long addr,YZF_RECV *area,uint16_t len);

YZF_RESULT ReadSectorFromMicroSD(long sector,YZF_RECV *area);// 读一个扇区
YZF_RESULT ReadSectorsFromMicroSD(long sector,YZF_RECV *area,uint16_t len);// 读一个扇区

uint8_t * sd_response_block_read(char cmd,uint16_t length,YZF_RECV *area);

YZF_RESULT yz_sd_spi_cmd0(YZF_SPI_POINTER spi);

YZF_RESULT yz_sd_spi_cmd8(YZF_SPI_POINTER spi);

YZF_RESULT yz_sd_spi_cmd9(YZF_SPI_POINTER spi);

YZF_RESULT yz_sd_spi_cmd10(YZF_SPI_POINTER spi);

YZF_RESULT yz_sd_spi_cmd12(YZF_SPI_POINTER spi);

YZF_RESULT yz_sd_spi_cmd13(YZF_SPI_POINTER spi);

YZF_RESULT yz_sd_spi_cmd17(YZF_SPI_POINTER spi,uint32_t sector,YZF_RECV *area);

YZF_RESULT yz_sd_spi_cmd18(YZF_SPI_POINTER spi,uint32_t sector,YZF_RECV *area,uint16_t len);

YZF_RESULT yz_sd_spi_cmd24(YZF_SPI_POINTER spi,uint32_t sector,YZF_RECV *area);

YZF_RESULT yz_sd_spi_cmd42(YZF_SPI_POINTER spi);

YZF_RESULT yz_sd_spi_cmd55(YZF_SPI_POINTER spi);

YZF_RESULT yz_sd_spi_cmd58(YZF_SPI_POINTER spi);

YZF_RESULT yz_sd_spi_cmd59(YZF_SPI_POINTER spi);

YZF_RESULT yz_sd_spi_acmd41(YZF_SPI_POINTER spi);

uint8_t *yz_sd_get_result();
#endif //YZFRAME_YZ_SPI_SD_H
